﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Douban.Api;
using Douban.Api.User;
using Douban.Core;
using Douban.Modules.Info.Views;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Unity;
using System.Windows;

namespace Douban.Modules.Info.ViewModels
{
    public class HeaderViewModel : ObservableObject, IHeaderViewModel
    {
        private readonly IUnityContainer _container;
        private readonly DoubanService _doubanService;
        private UserDetail _userDetail;
        private ICommand _openAccountCommand;

        public HeaderViewModel(IUnityContainer container, IHeaderView view, DoubanService doubanService)
        {
            _container = container;
            _doubanService = doubanService;
            View = view;
            View.Context = this;

            InitializeHeader();
        }

        public IHeaderView View { get; set; }

        public string IconUrl 
        {
            get
            {
                return _userDetail.IconUri;
            }
            set
            {
                if(_userDetail.IconUri != value)
                {
                    _userDetail.IconUri = value;
                    NotifyPropertyChanged("IconUrl");
                }
            }
        }

        public string Name
        {
            get { return _userDetail.Name; }
            set
            {
                if(_userDetail.Name != value)
                {
                    _userDetail.Name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }
        
        public string Signature
        {
            get { return _userDetail.Signature; }
            set
            {
                if (_userDetail.Signature != value)
                {
                    _userDetail.Signature = value;
                    NotifyPropertyChanged("Signature");
                }
            }
        }	

        public ICommand OpenAccountCommand
        {
            get
            {
                if (_openAccountCommand == null)
                    _openAccountCommand = new DelegateCommand<object>(
                        obj => _container.Resolve<InfoController>().CheckInfo(_userDetail.Id));

                return _openAccountCommand;
            }
        }

        private void InitializeHeader()
        {
            _userDetail = _doubanService.UserService.GetCurrentUser();
        }
    }
}
